package com.idealighter.game.dictionary.dic;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.core.DictionaryTable;
import com.idealighter.game.dictionary.domain.FruitslotIconDomain;
import com.idealighter.game.dictionary.mapper.FruitslotIconMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Singleton
public class FruitslotIconDic implements DictionaryTable<FruitslotIconDomain> {

  private FruitslotIconMapper mapper;
  private List<FruitslotIconDomain> list;
  private Map<Integer, FruitslotIconDomain> map;

  // 合计权重
  private int totalWeight = 0;

  @Inject
  public FruitslotIconDic(FruitslotIconMapper mapper) {
    this.mapper = mapper;
    load();
  }

  @Override
  public List<FruitslotIconDomain> list() {
    return list;
  }

  @Override
  public Map<Integer, FruitslotIconDomain> map() {

    return map;
  }

  @Override
  public void load() {
    AtomicInteger totalWeightTmp = new AtomicInteger(0);

    this.list = mapper.selectAll();
    Map<Integer, FruitslotIconDomain> map = new HashMap<>();
    this.list.forEach(val -> {
      map.put(val.getId(), val);
      totalWeightTmp.getAndAdd(val.getWeight());
    });

    this.map = map;
    this.totalWeight = totalWeightTmp.intValue();
  }

  @Override
  public FruitslotIconDomain get(int id) {

    return map.get(id);
  }

  public int getTotalWeight() {
    return totalWeight;
  }

}
